/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
YAHOO.namespace( 'shoutbox' );
YAHOO.shoutbox = new Object();
YAHOO.shoutbox.periodicUpdateId = -1;
YAHOO.shoutbox.getChatFile = "get_chat.php";
YAHOO.shoutbox.addChatFile = "add_chat.php";
YAHOO.shoutbox.isInitialized = false;

YAHOO.shoutbox.initChat = function() {
	// If the message container is not present we can't initialize
	if( document.getElementById( "message-container" ) ) {
		periodicUpdateId = setTimeout( 'YAHOO.shoutbox.periodicUpdate();', 15000 );
		YAHOO.shoutbox.updateChat( true );
		YAHOO.shoutbox.isInitialized = true;
	}
}

/*
 * This function stops the periodic updates of content 
 * and marks the shoutbox as not initialized
 */
YAHOO.shoutbox.stopChat = function() {
	clearTimeout( YAHOO.shoutbox.periodicUpdateId );
	YAHOO.shoutbox.isInitialized = false;
}

/*
 * Use this function to set a different file name/path to retrieve the messages 
 */
YAHOO.shoutbox.setGetChatFile = function( file ) {
	YAHOO.shoutbox.getChatFile = file;
}

/*
 * Use this function to set a different file name/path to add messages 
 */
YAHOO.shoutbox.setAddChatFile = function( file ) {
	YAHOO.shoutbox.addChatFile = file;
}

YAHOO.shoutbox.updateChat = function( update, force_channel ) {
	var howMany = document.getElementById( 'display-setting' );
	var page = document.getElementById( 'page-setting' );
	var add = '';
	
	if( force_channel ) {
		add = '&channel_id=' + force_channel;
	}
	else {
		var channel_id = document.getElementById( 'channel-id' );
		if( channel_id && channel_id.innerHTML ) {
			add = '&channel_id=' + channel_id.innerHTML;
		}
		else {
			add = '&initialize=true';
		}
	}
	
	if( update ) {
		add += '&update=force';
	}
	
	if( page && howMany ) {
		YAHOO.util.Connect.asyncRequest( 'GET', YAHOO.shoutbox.getChatFile + '?page=' + page.innerHTML + '&amount=' + howMany.value + add, YAHOO.shoutbox.getChatCallback, null );
	}
}

YAHOO.shoutbox.periodicUpdate = function() {
	YAHOO.shoutbox.updateChat();
	var howMany = document.getElementById( 'display-setting' );
	if( howMany ) {
		howMany = howMany.value;
		// To save some bandwidth: 
		// the more messages at the same time you watch the longer it takes to refresh
		var delay = 10000;
		if( howMany == '20' ) {
			delay = 20000;
		}
		else if( howMany == '50' ) {
			delay = 50000;
		}
		YAHOO.shoutbox.periodicUpdateId = setTimeout( 'YAHOO.shoutbox.periodicUpdate();', delay );
	}
}

YAHOO.shoutbox.getChatSuccess = function( o ) {
	if( o.responseText != 'up-to-date' ) {
		var container = document.getElementById( 'message-container' );
		container.innerHTML = o.responseText;
		// It is not so easy to get the proper height, so we use a very high number to auto scroll down
		var attributes = {
			scroll: { to: [0, 9999999] }			
		};
		var animation = new YAHOO.util.Scroll( "chat-message-container", attributes );
		animation.animate();
	}
}

YAHOO.shoutbox.getChatFailure = function( o ) {
	clearTimeout( YAHOO.shoutbox.periodicUpdateId );
}

YAHOO.shoutbox.getChatCallback = {
	success: YAHOO.shoutbox.getChatSuccess, 
	failure: YAHOO.shoutbox.getChatFailure,
	cache: false
};

/***************************************************************************
    Copyright 2008 Bastiaan Grutters
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published 
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
***************************************************************************/

YAHOO.shoutbox.addMessage = function() {
	var text = document.getElementById( 'write-area' );
	if( text.value.length < 2 ) {
		alert( "Please, write a message first" );
	}
	else {
		text = text.value.replace( /\+/g, '%2B' );		
		text = encodeURIComponent( text );
		var channel = document.getElementById( 'channel-id' );
		var add = '';
		if( channel && channel.innerHTML ) {
			add = '&channel_id=' + channel.innerHTML;
			YAHOO.util.Connect.asyncRequest( 'POST', YAHOO.shoutbox.addChatFile, YAHOO.shoutbox.addMessageCallback, 'text=' + text + add );
		}
		else {
			alert( 'Wait for the channel to load, before adding messages.' );
		}
	}
}

YAHOO.shoutbox.addMessageSuccess = function( o ) {
	var text = document.getElementById( 'write-area' );
	text.value = '';
	YAHOO.shoutbox.updateChat( true );
}

YAHOO.shoutbox.addMessageFailure = function( o ) {
	/*alert( "An error occured while trying to add your message, please try again.\r\n" +
			"\r\n" +
			"Error message: " + o.statusText );*/
}

YAHOO.shoutbox.addMessageCallback = {
	success: YAHOO.shoutbox.addMessageSuccess, 
	failure: YAHOO.shoutbox.addMessageFailure
};

YAHOO.shoutbox.nextMessages = function() {
	var page = document.getElementById( 'page-setting' );
	var value = page.innerHTML;
	if( value > 1 ) {
		value --;
	}
	page.innerHTML = value;
	YAHOO.shoutbox.updateChat( true );
}

YAHOO.shoutbox.previousMessages = function() {
	var page = document.getElementById( 'page-setting' );
	var value = page.innerHTML;
	value ++;
	page.innerHTML = value;
	YAHOO.shoutbox.updateChat( true );
}

YAHOO.util.Event.addListener( window, 'load', YAHOO.shoutbox.initChat );